<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Comment extends CI_Controller {

	function __construct(){
		parent::__construct();
		$this->load->model('production_model');
		$this->load->model('comment_model');
	}
	public function action()
	{
		$data['success'] = $this->session->flashdata('success');
		$data['error'] = $this->session->flashdata('error');
		$id = (int)$this->uri->segment(3);
		if(!$id) show_error('错误的操作');

		$p = $this->production_model->get_production_byid($id);
		if(!$p) show_error('该商品可能已经被删除');
		

		$data['p'] = $p;
		$this->load->library('form_validation');
		$this->form_validation->set_rules('content', '评论内容', 'trim|required|xss_clean');
		if($this->form_validation->run()){
			auth('/comment/action/'.$id);
			$r = $this->comment_model->action($id);
			if($r['status']){
				$this->session->set_flashdata('success','评论成功！');
			}
			else{
				$this->session->set_flashdata('error',$r['msg']);
			}
				redirect(current_url(),'reflash');
		}

		
		$start = $this->uri->segment(4,0);
		$data['c'] = $this->comment_model->get_comment($id,$start,PAGENUM + 10);
		$data['num'] = $this->comment_model->get_comment_num($id);

		//pagenation
		$this->load->library('pagination');
		$config['base_url'] = '/comment/action/' . $id . '/';
		$config['total_rows'] = $data['num'];
		$config['per_page'] = PAGENUM + 10; 
		$config['full_tag_open'] = '<p class="page">';
		$config['full_tag_close'] = '</p>';
		$config['uri_segment'] = 4;
		$config['first_link'] = '首页';
		$config['last_link'] = '尾页';
		$config['cur_tag_open'] = '<b>';
		$config['cur_tag_close'] = '</b>';
		$config['next_link'] = '下一页';
		$config['prev_link'] = '上一页';
		$this->pagination->initialize($config); 
		$data['page'] = $this->pagination->create_links();


		$this->load->view('comment.tpl',$data);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */